package com.car.result;

import lombok.Data;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    /**
     * 返回码
     */
    private Integer code;
    /**
     * 返回信息
     */
    private String message;
    /**
     * 返回数据
     */
    private T data;

    /**
     * 私有化构造方法，禁止在其它类创建对象
     */
    private Result() {
    }

    /**
     * 执行成功 不返回数据
     */
    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage("操作成功");
        result.setData(null);
        return result;
    }

    /**
     * 执行成功 返回自定义信息提示
     */
    public static <T> Result<T> ok(String message) {
        Result<T> result = new Result<>();
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    /**
     * 执行成功 返回数据
     */
    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage("操作成功");
        result.setData(data);
        return result;
    }

    /**
     * 执行成功 返回信息和数据
     */
    public static <T> Result<T> ok(String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    /**
     * 执行失败 返回默认数据
     */
    public static <T> Result<T> fail() {
        Result<T> result = new Result<>();
        result.setCode(HttpServletResponse.SC_NOT_IMPLEMENTED);
        result.setMessage("操作失败");
        result.setData(null);
        return result;
    }


    /**
     * 执行失败 只返回消息
     */
    public static <T> Result<T> fail(String message) {
        Result<T> result = new Result<>();
        result.setCode(HttpServletResponse.SC_NOT_IMPLEMENTED);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    /**
     * 执行失败 只返回状态码和消息
     */
    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        return result;
    }
}
